package com.lanou.tk.tourism.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.tk.tourism.R;
import com.lanou.tk.tourism.adapter.HotCountryRecyclerViewAdapter;
import com.lanou.tk.tourism.entity.World;

import java.io.Serializable;
import java.util.List;

/**
 * Created by dllo on 15/11/4.
 */
public class HotCountryFragment extends Fragment{
    private TextView hotCountryTitle_Tv, CountryCNNTv;
    private ImageView countryImgIv;

    private HotCountryRecyclerViewAdapter hcRecyclerViewAdapter;
    private RecyclerView recyclerView;

    public static HotCountryFragment newInstance(List<World.DataEntity.HotCountryEntity> list, String id){
        HotCountryFragment f = new HotCountryFragment();
        Bundle args = new Bundle();
        args.putSerializable("list", (Serializable) list);
        args.putSerializable("id", id);
        f.setArguments(args);
        return f;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_hot_country, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        hotCountryTitle_Tv = (TextView) view.findViewById(R.id.hotCountryTitle_Tv);
        countryImgIv = (ImageView) view.findViewById(R.id.countryImgIv);
        CountryCNNTv = (TextView) view.findViewById(R.id.CountryCNNTv);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView = (RecyclerView) view.findViewById(R.id.hotCountryRecyclerView);
        recyclerView.setLayoutManager(gridLayoutManager);
        hcRecyclerViewAdapter = new HotCountryRecyclerViewAdapter(getActivity());
        recyclerView.setAdapter(hcRecyclerViewAdapter);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
//        getColor();
    }

    private void initData() {
        hotCountryTitle_Tv.setText(getArguments().getString("id") + "热门目的地");

        List<World.DataEntity.HotCountryEntity> wdh = (List<World.DataEntity.HotCountryEntity>) getArguments().getSerializable("list");
        hcRecyclerViewAdapter.addData(wdh);
    }

//    private void getColor() {

//        Palette.from(bmp).generate(new Palette.PaletteAsyncListener() {
//            @Override
//            public void onGenerated(Palette palette) {
//                hotCountryTitle_Tv.setBackgroundColor(R.id.countryImgIv);
//            }
//        });

//        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.id.countryImgIv);
//        Palette.generateAsync(bmp, new Palette.PaletteAsyncListener() {
//            @Override
//            public void onGenerated(Palette palette) {
//                Palette.Swatch swatch = palette.getVibrantSwatch();
//                if (null != swatch) {
//                    CountryCNNTv.setBackgroundColor(swatch.getRgb());
//                }
//            }
//        });
//
//    }


}
